package com.cahnggou.file.controller;

import com.cahnggou.file.FastDFSFile;
import com.cahnggou.util.FastDFSUtil;
import com.thoughtworks.xstream.core.util.FastField;
import entity.Result;
import entity.StatusCode;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

/**
 * @Author: HuxiWang
 * @Date: Create in 15:16 2021/1/3
 * @Description: fastdfs文件上传
 */
@RequestMapping("/upload")
@CrossOrigin
@RestController
public class FileUploadController {

    @PostMapping
    public Result upload(@RequestParam("file") MultipartFile file) throws Exception {
        // 调用fastdfs的工具类
        FastDFSFile fastDFSFile = new FastDFSFile(
                file.getOriginalFilename(),
                file.getBytes(),
                // 获取文件扩展名
                StringUtils.getFilenameExtension(file.getOriginalFilename()));
        // 返回上传文件的信息
        String[] uploads = FastDFSUtil.upload(fastDFSFile);

        // 拼接访问地址  url = http://192.168.160.128:8080/组名/位置/文件名  先将请求访问nginx
        String url = "http://192.168.160.128:8080/" + uploads[0] + "/" + uploads[1];
        return new Result(true, StatusCode.OK, "上传成功", url);
    }

}














